package jvalhondo.android.CuestaDelSol.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMSEdit extends Activity {
        // for fields Person, Description, Amount
        private EditText mTitleText;
        private EditText mDescriptionText;
        
        private Long mRowId;
        private SMSDbAdapter mDbHelper;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                
                mDbHelper = new SMSDbAdapter(this);
                mDbHelper.open();
                
                setContentView(R.layout.sms_edit);
                setTitle(R.string.edit_sms_title);
                
                // capture our Text elements
                mTitleText = (EditText) findViewById(R.id.person);
                mDescriptionText = (EditText) findViewById(R.id.description);
                
                Button sendSMSButton = (Button) findViewById(R.id.sendSMS);
                Button confirmButton = (Button) findViewById(R.id.confirm);
                
                
                mRowId = (savedInstanceState == null) ? null :
            (Long) savedInstanceState.getSerializable(SMSDbAdapter.KEY_ROWID);
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(SMSDbAdapter.KEY_ROWID)
                                    : null;
        }
        
        populateFields();
        
        sendSMSButton.setOnClickListener(new View.OnClickListener() {
                        
                    public void onClick(View view) {
                        
                        try {
                                     Intent sendSMSIntent =  new Intent(Intent.ACTION_VIEW);
                                     sendSMSIntent.putExtra("address","648053449");
                                     sendSMSIntent.putExtra("sms_body", mDescriptionText.getText().toString()); 
                                     sendSMSIntent.setType("vnd.android-dir/mms-sms");
                                     startActivity(sendSMSIntent);

                                } catch (Exception e) {
                                        Toast.makeText(getApplicationContext(),
                                                "Fallo en el envio, intentelo de nuevo",
                                                Toast.LENGTH_LONG).show();
                                        e.printStackTrace();
                                }
                    }    
                });
        
                confirmButton.setOnClickListener(new View.OnClickListener() {
                        
                    public void onClick(View view) {
                        
                        if( someFieldsEmpty() == true ){
                                
                                showErrorMessage("Error", "Los campos no pueden estar vac�os.");
                                
                        } else{
                                setResult(RESULT_OK);
                                Toast.makeText(getBaseContext(), "Guardado",Toast.LENGTH_SHORT).show();
                                finish();
                        }
                    }    
                });
        } // close onCreate method
        
        // check fields are not empty before confirm
        private boolean someFieldsEmpty() {
                
                if( (mTitleText.getText().toString()).equals("") || (mTitleText.getText().toString()).equals(" ")  ||
                        (mDescriptionText.getText().toString()).equals("") || (mDescriptionText.getText().toString()).equals(" ") ){
                        
                        return true;
                }
                return false;
        }
        
        // is call when an error should be alert
        private void showErrorMessage(String title, String content) {
                AlertDialog alertDialog;
                
                alertDialog = new AlertDialog.Builder(this).create();
                alertDialog.setTitle(title);
                alertDialog.setMessage(content);
                alertDialog.show();
        }   
        
        private void populateFields() {
           if (mRowId != null) {
                  Cursor smsCursor = mDbHelper.fetchNote(mRowId);
                  startManagingCursor(smsCursor);
                  mTitleText.setText(smsCursor.getString(
                              smsCursor.getColumnIndexOrThrow(SMSDbAdapter.KEY_TITLE)));
                  mDescriptionText.setText(smsCursor.getString(
                                          smsCursor.getColumnIndexOrThrow(SMSDbAdapter.KEY_DESCRIPTION)));
           }
    }
                
        
        @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveState();
        outState.putSerializable(SMSDbAdapter.KEY_ROWID, mRowId);
    }
         
        private void saveState() {
                 String title = mTitleText.getText().toString();
             String description = mDescriptionText.getText().toString();
             
             if( someFieldsEmpty() == true ) {
                 
                 showErrorMessage("Error", "Algunos campos est�n vac�os. SMS no guardado");
             }
             
             else {
                 if (mRowId == null) {
                        long id = mDbHelper.createSMS(title, description);
                        if (id > 0) {
                           mRowId = id;
                        }
                     } else {
                       mDbHelper.updateLoan(mRowId, title, description);
                     } 
             }
        }

        @Override
        protected void onPause() {
                super.onPause();
                saveState();
        }
         
         @Override
         protected void onResume() {
                super.onResume();
                populateFields();
         }
}